$(document).ready(function() {
    // 渲染轮播图
    function renderBanner() {
        const sliderWrapper = $('#sliderWrapper');
        const sliderDots = $('#sliderDots');
        
        // 渲染轮播图片
        bannerData.forEach(banner => {
            const slideHtml = `
                <div class="slider-item">
                    <a href="${banner.link}">
                        <img src="${banner.image}" alt="${banner.title}">
                    </a>
                </div>
            `;
            sliderWrapper.append(slideHtml);
            
            // 渲染指示点
            const dotHtml = `<span class="dot"></span>`;
            sliderDots.append(dotHtml);
        });
        
        // 激活第一个指示点
        $('.dot').first().addClass('active');
    }
    
    // 初始化轮播图
    renderBanner();
    
    let currentSlide = 0;
    const slideCount = bannerData.length;
    const sliderWrapper = $('.slider-wrapper');
    
    // 自动播放
    function autoPlay() {
        currentSlide = (currentSlide + 1) % slideCount;
        updateSlider();
    }
    
    let autoPlayTimer = setInterval(autoPlay, 3000);
    
    // 更新轮播图位置和指示点
    function updateSlider() {
        sliderWrapper.css('transform', `translateX(-${currentSlide * 25}%)`);
        $('.dot').removeClass('active').eq(currentSlide).addClass('active');
    }
    
    // 点击指示点切换
    $(document).on('click', '.dot', function() {
        currentSlide = $(this).index();
        updateSlider();
        resetTimer();
    });
    
    // 点击箭头切换
    $('.arrow-left').click(function() {
        currentSlide = (currentSlide - 1 + slideCount) % slideCount;
        updateSlider();
        resetTimer();
    });
    
    $('.arrow-right').click(function() {
        currentSlide = (currentSlide + 1) % slideCount;
        updateSlider();
        resetTimer();
    });
    
    // 重置定时器
    function resetTimer() {
        clearInterval(autoPlayTimer);
        autoPlayTimer = setInterval(autoPlay, 3000);
    }
    
    // 鼠标悬停时暂停自动播放
    $('.banner-slider').hover(
        function() {
            clearInterval(autoPlayTimer);
        },
        function() {
            autoPlayTimer = setInterval(autoPlay, 3000);
        }
    );
}); 